import {HttpClient } from '@angular/common/http';
import {Injectable} from '@angular/core';
import {Observable} from "rxjs/Observable";
import * as moment from 'moment';
import {PageParamsModel} from "../../../model/common/page-params.model";
import {LoadingService} from "../../../services/common/loading.service";
import {PageInfoModel} from "../../../model/common/page-info.model";

@Injectable()
export class TalentPoolProvider {

    constructor(public http: HttpClient) {
    }

    convertParamsTimeToString<T>(tar,propertyArr:Array<string>):T{
        propertyArr.map((v1)=>{
            if(tar[v1]){
                tar[v1] = moment(tar[v1]*1000).format('YYYY-MM-DD HH:mm:ss');
            }
        });
        console.log(tar);
        return tar;
    }

    getDataByPage(pageParams:PageParamsModel,loading?:LoadingService) {

        if(loading)
            loading.showLoading();
        return Observable.create(ob => {
            let p:any = {};
            p.pageNum = String(pageParams.pageNum);
            p.pageSize = String(pageParams.pageSize);
            if(pageParams.orderName)p.orderName = pageParams.orderName;
            if(pageParams.orderType)p.orderType = pageParams.orderType;
            if(pageParams.condition){
                let cond = pageParams.condition;
                if(cond.source){p.source = cond.source;}
                if(cond.starttime){p.starttime = cond.starttime;}
                if(cond.endtime){p.endtime = cond.endtime;}
                if(cond.process){p.process = cond.process;}
                if(cond.keyword){p.keyword = cond.keyword;}
                if(cond.processStartTime){p.processStartTime = cond.processStartTime;}
                if(cond.processEndTime){p.processEndTime = cond.processEndTime;}
            }

            //this.convertParamsTimeToString(p,['starttime','endtime','processStartTime','processEndTime']);

            this.http.get('/recruit/target/v1/list',{
                params:p
            })
            .subscribe((d:any) => {

                if(d && d.list && d.list.length){
                    for(let i = 0; i < d.list.length; i++){
                        d.list[i].createTime = moment(new Date(d.list[i].createTime)).format('YYYY/MM/DD');
                    }

                    //region 模拟数据
                    // let source = d.list;
                    // let start = pageParams.pageSize * (pageParams.pageNum - 1);
                    // let end = start + pageParams.pageSize;
                    //
                    // let talentList = [];
                    // for(let o = 0; o < 14; o++){
                    //     for(let i = 0; i < source.length; i++){
                    //         talentList.push({
                    //             name:`${o * source.length + i}${source[i].name}`,
                    //             age:source[i].age,
                    //             birthDate:source[i].birthDate,
                    //             createTime:moment(new Date(source[i].createTime)).format('YYYY/MM/DD'),
                    //             isNew:0,
                    //             mobileList:source[i].mobileList,
                    //             process:source[i].process,
                    //             recruitId:source[i].recruitId,
                    //             sex:source[i].sex,
                    //             source:source[i].source,
                    //         });
                    //     }
                    // }
                    //
                    // const pageInfo = d as PageInfoModel;
                    // pageInfo.total = talentList.length;
                    // pageInfo.pages = Math.ceil(pageInfo.total / pageInfo.pageSize);
                    //
                    // let currentPageData = talentList.slice(start,end);
                    // ob.next({
                    //     list:currentPageData,
                    //     pageNum:pageInfo.pageNum,
                    //     pageSize:pageInfo.pageSize,
                    //     pages:pageInfo.pages,
                    //     total:pageInfo.total
                    // });
                    //endregion

                }

                ob.next(d);
                if(loading)
                    loading.dismissLoading();
            },() => {
                ob.next({list:null});
                if(loading)
                    loading.dismissLoading();
            });

        });
    }

    public processes = [
        {
            title: "人才库",
            id: 0
        },
        {
            title: "候选人",
            id: 1
        },
        {
            title: "虚拟工号",
            id: 2
        },
        {
            title: "ITC结训",
            id: 3
        },
        {
            title: "入司",
            id: 4
        }
    ];

}
